package com.aramis.mydytt.ui.dialog

import android.app.Activity
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.aramis.mydytt.R
import com.aramis.mydytt.ui.bean.DyttMovieDetailBean
import com.aramis.mydytt.ui.bean.MovieDetailBean
import com.aramis.mydytt.ui.bean.MovieDetailDownListBean
import com.aramis.mydytt.ui.bean.MovieField

/**
 * Description: DetailDownloadDialog
 * @author lizhidan
 * Created: 2021/5/8
 */
class DetailDownloadDialog(activity: Activity) : BottomDialog(activity) {
    private var movieDetailBean: MovieDetailBean? = null
    private var dyttDetail: DyttMovieDetailBean? = null
    private var movieNameView: TextView? = null
    private var downloadListView: RecyclerView? = null
    private var cate = -1
    private var adapter = DialogDownloadListAdapter()

    private var sourceView1: View? = null
    private var sourceView2: View? = null

    override fun getLayoutId(): Int = R.layout.dialog_detail_download

    fun setMovieName(movieName: String) {
        movieNameView?.text = movieName
    }

    fun setData(bean: MovieDetailBean?) {
        movieDetailBean = bean
        cate = if (bean?.category != null && bean.category.isNotEmpty()) bean.category[0].id else -1
        adapter.setCate(cate)
    }

    fun setOnDownloadListItemClickListener(onItemClickListener: ((MovieDetailDownListBean) -> Unit)?) {
        adapter.onItemClickListener = onItemClickListener
    }

    fun setDyttDownloadData(dyttDetail: DyttMovieDetailBean?) {
        this.dyttDetail = dyttDetail
        sourceView2?.visibility = if (dyttDetail != null) View.VISIBLE else View.GONE
    }

    override fun onInit() {
        super.onInit()
        movieNameView = findView(R.id.text_movie_name)
        findView<View>(R.id.image_close)?.setOnClickListener {
            dismiss()
        }
        downloadListView = findView(R.id.list_download)
        sourceView1 = findView(R.id.text_dialog_download_source1)
        sourceView2 = findView(R.id.text_dialog_download_source2)
        underCoverHeight(mDecorView?.findViewById(R.id.layout_dialog_download))
        sourceView1?.setOnClickListener {
            setDyttModeData(false)
        }
        sourceView2?.setOnClickListener {
            setDyttModeData(true)
        }
    }

    override fun show() {
        refreshData()
        super.show()
    }

    private fun setDyttModeData(dyttMode: Boolean) {
        sourceView1?.setBackgroundResource(if (dyttMode) R.drawable.bg_detail_download_item else R.drawable.bg_detail_download_src_selected)
        sourceView2?.setBackgroundResource(if (dyttMode) R.drawable.bg_detail_download_src_selected else R.drawable.bg_detail_download_item)
        downloadListView?.layoutManager = if (dyttMode || cate == MovieField.CATE_MOVIE) {
            LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        } else {
            GridLayoutManager(activity, 4)
        }
        if (dyttMode) {
            adapter.setData(dyttDetail?.toMovieDetailDownListBeanList(cate))
        } else {
            adapter.setData(if (cate > 0) movieDetailBean?.btbo_downlist else null)
        }
    }

    private fun refreshData() {
        downloadListView?.adapter = adapter
        setDyttModeData(false)
    }
}